<template>
  <div class="app-container">
    <div>
      <el-button type="danger" round>全部</el-button>
      <el-button type="danger" round>授信</el-button>
      <el-button type="danger" round>未授信</el-button>
    </div>
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="商户名称" prop="businessName">
        <el-date-picker clearable size="small"
                        v-model="queryParams.businessName"
                        type="businessName"
                        value-format="yyyy-MM-dd"
                        placeholder="选择商户名称">
        </el-date-picker>
      </el-form-item><br>
      <el-form-item label="授信总额度" prop="registerCapital">
        <el-col :span="11">
          <el-input v-model="queryParams.registerCapital"></el-input>
        </el-col>
        <el-col class="line" :span="2">-</el-col>
        <el-col :span="11">
          <el-input v-model="queryParams.registerCapital"></el-input>
        </el-col>
      </el-form-item>
      <el-form-item label="已用授信额度(%)" prop="hlUsedCreditline">
        <el-col :span="11">
          <el-input v-model="queryParams.hlUsedCreditline"></el-input>
        </el-col>
        <el-col class="line" :span="2">%-</el-col>
        <el-col :span="11">
          <el-input v-model="queryParams.hlUsedCreditline">%</el-input>
        </el-col>
      </el-form-item><br>
      <el-form-item label="月授信总额度" prop="hlMonthlyTotalCreditline">
        <el-col :span="11">
          <el-input v-model="queryParams.hlMonthlyTotalCreditline"></el-input>
        </el-col>
        <el-col class="line" :span="2">-</el-col>
        <el-col :span="11">
          <el-input v-model="queryParams.hlMonthlyTotalCreditline"></el-input>
        </el-col>
      </el-form-item>
      <el-form-item label="已用授信额度(%)" prop="hlMonthlyUsedCreditline">
        <el-col :span="11">
          <el-input v-model="queryParams.hlMonthlyUsedCreditline"></el-input>
        </el-col>
        <el-col class="line" :span="2">%-</el-col>
        <el-col :span="11">
          <el-input v-model="queryParams.hlMonthlyUsedCreditline">%</el-input>
        </el-col>
      </el-form-item><br>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['fengkong:credit:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['fengkong:credit:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['fengkong:credit:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['fengkong:credit:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="creditList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="公寓名称" align="center" prop="businessName" />
      <el-table-column label="注册日期" align="center" prop="registerDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.registerDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="注册资本(万元）" align="center" prop="registerCapital" />
      <el-table-column label="授信状态" align="center" prop="creditStaus"/>
      <el-table-column label="应花分期使用" align="center" prop="appid"/>
      <el-table-column label="总授信额度" align="center" prop="hlTotalCreditline" />
      <el-table-column label="已用授信额度" align="center" prop="hlUsedCreditline" />
      <el-table-column label="月授信额度" align="center" prop="hlMonthlyTotalCreditline" />
      <el-table-column label="月已用授信额度" align="center" prop="hlMonthlyUsedCreditline" />
      <el-table-column label="资金方名称" align="center" prop="companyName" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['fengkong:credit:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['fengkong:credit:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改风控公寓授信主对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="公寓名称" prop="businessId">
          <el-input v-model="form.businessId" placeholder="公寓名称" />
        </el-form-item>
        <el-form-item label="商户注册日期" prop="registerDate">
          <el-date-picker clearable size="small"
            v-model="form.registerDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择商户注册日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="注册资本(万元）" prop="registerCapital">
          <el-input v-model="form.registerCapital" placeholder="注册资本(万元）" />
        </el-form-item>
        <el-form-item label="授信状态" prop="creditStaus">
          <el-input v-model="form.creditStaus" placeholder="授信状态" />
        </el-form-item>
        <el-form-item label="应花分期使用" prop="appid">
          <el-input v-model="form.appid" placeholder="请输入应花分期使用" />
        </el-form-item>
        <el-form-item label="海链总授信额度" prop="hlTotalCreditline">
          <el-input v-model="form.hlTotalCreditline" placeholder="请输入海链总授信额度" />
        </el-form-item>
        <el-form-item label="海链已用授信额度" prop="hlUsedCreditline">
          <el-input v-model="form.hlUsedCreditline" placeholder="请输入海链已用授信额度" />
        </el-form-item>
        <el-form-item label="海链月授信额度" prop="hlMonthlyTotalCreditline">
          <el-input v-model="form.hlMonthlyTotalCreditline" placeholder="请输入海链月授信额度" />
        </el-form-item>
        <el-form-item label="海链月已用授信额度" prop="hlMonthlyUsedCreditline">
          <el-input v-model="form.hlMonthlyUsedCreditline" placeholder="请输入海链月已用授信额度" />
        </el-form-item>

        <el-form-item label="创建用户id" prop="capitalId">
          <el-input v-model="form.capitalId" placeholder="请输入创建用户id" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCredit, getCredit, delCredit, addCredit, updateCredit } from "@/api/monitor/credit";

export default {
  name: "Credit",
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 风控公寓授信主表格数据
      creditList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        businessId: null,
        registerDate: null,
        registerCapital: null,
        creditStaus: null,
        appid: null,
        hlTotalCreditline: null,
        hlUsedCreditline: null,
        hlMonthlyTotalCreditline: null,
        hlMonthlyUsedCreditline: null,
        capitalId:null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        businessId: [
          { required: true, message: "商户id不能为空", trigger: "blur" }
        ],
        creditStaus: [
          { required: true, message: "授信状态，1：已授信、2：未授信不能为空", trigger: "blur" }
        ],
        createUserid: [
          { required: true, message: "创建用户id不能为空", trigger: "blur" }
        ],
        createTime: [
          { required: true, message: "创建时间不能为空", trigger: "blur" }
        ],
        updateUserid: [
          { required: true, message: "更新用户id不能为空", trigger: "blur" }
        ],
        updateTime: [
          { required: true, message: "更新时间不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询风控公寓授信主列表 */
    getList() {
      this.loading = true;
      listCredit(this.queryParams).then(response => {
        console.log(this.queryParams);
        this.creditList = response.rows;
        this.total = response.total;
        this.loading = false;
        this.creditList.map(function (value){
          if (value.creditStaus==1){
            value.creditStaus='授信'
          }else {
            value.creditStaus='未授信'
          }
        })
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        businessId: null,
        registerDate: null,
        registerCapital: null,
        creditStaus: null,
        appid: null,
        hlTotalCreditline: null,
        hlUsedCreditline: null,
        hlMonthlyTotalCreditline: null,
        hlMonthlyUsedCreditline: null,
        capitalId:null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加风控公寓授信主";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getCredit(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改风控公寓授信主";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateCredit(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCredit(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除风控公寓授信主编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delCredit(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('fengkong/credit/export', {
        ...this.queryParams
      }, `fengkong_credit.xlsx`)
    }
  }
};
</script>
